import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

// 声明路由元数据类型
declare module "vue-router" {
  interface RouteMeta {
    title: string;
    description?: string;
  }
}

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "home",
    component: () => import("@/views/Home.vue"),
    meta: {
      title: "首页",
      description: "首页",
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    }
    return { top: 0 };
  },
});

// 全局导航守卫
router.beforeEach((to) => {
  document.title = `${to.meta.title} - 乱写`;
});

export default router;
